//
//  HomeCell.swift
//  WBSwift
//
//  Created by maker on 2016/11/16.
//  Copyright © 2016年 maker. All rights reserved.
//

import UIKit
import SDWebImage

class HomeCell: UITableViewCell {
    //MARK:==========控件属性==========
    
    @IBOutlet weak var icon: UIImageView!                   //头像
    @IBOutlet weak var vertifyIcon: UIImageView!            //认证类型图标
    @IBOutlet weak var nickNameLbl: UILabel!                //昵称
    @IBOutlet weak var vipIcon: UIImageView!                // VIP
    @IBOutlet weak var timeLbl: UILabel!                    //时间
    @IBOutlet weak var sourceLbl: UILabel!                  //来源
    @IBOutlet weak var contentLbl: UILabel!                 //正文
    @IBOutlet weak var collectionView: UICollectionView!    //配图
    @IBOutlet weak var replyContenLbl: UILabel!             //转发正文
    @IBOutlet weak var backView: UIView!                    //背景图
    @IBOutlet weak var bottomToolView: UIView!              //底部工具条
    
    
    //写一个属性
    var itemWidth :CGFloat {
        return (screenWidth - 2*margin - 2*itemMargin)/3
    }
    
    
    @IBOutlet weak var contentLblWidth: NSLayoutConstraint!
    @IBOutlet weak var collH: NSLayoutConstraint!
    @IBOutlet weak var collW: NSLayoutConstraint!
    @IBOutlet weak var collBot: NSLayoutConstraint!
    @IBOutlet weak var replyContentLblTop: NSLayoutConstraint!

    
    var model : StatusViewModel?{
        didSet{
            //1.判空
            guard let model = model else {
                return
            }
            icon.sd_setImage(with: URL(string: model.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            vertifyIcon.image = model.verifyImage
            nickNameLbl.text = model.status?.user?.screen_name
            vipIcon.image = model.vipImage
            timeLbl.text = model.timeText
            sourceLbl.text = model.sourceText
            contentLbl.text = model.status?.text
            contentLbl.attributedText = ShowEmotionTool.shareIntance.getAttrStr(model.status?.text, font: contentLbl.font)
            nickNameLbl.textColor = model.vipImage == nil ? UIColor.black : UIColor.orange
            
            //1.计算图片的宽度和高度
            if let count = model.imaURLs?.count {
                let collectionSize = calculateSize(count)
                collW.constant = collectionSize.width
                collH.constant = collectionSize.height
            }
            
            //2.给图片视图设置数据源
            let picCollectView = collectionView as! PicCollection
            if let urls = model.imaURLs {
                picCollectView.urls = urls
            }
            
            //3.给转发填充数据
            if let retweeted = model.status?.retweeted_status {
                backView.isHidden = false
                guard let screenName = retweeted.user?.screen_name else {
                    return
                }
                guard let text = retweeted.text  else {
                    return
                }
                let statusText = "@" + "\(screenName): " + text
                
                replyContenLbl.attributedText = ShowEmotionTool.shareIntance.getAttrStr(statusText, font: replyContenLbl.font)
                
                 //如果有转发回复约束
                replyContentLblTop.constant = 15
            }else{
                replyContenLbl.text = nil
                replyContentLblTop.constant = 0
                backView.isHidden = true
            }
            
            //4.计算行高(防止重复计算判断高度是否为0)
            if model.rowHeight == 0{
                //4.1强制布局
                layoutIfNeeded()
                
                //4.2取出 tool 最大高度并赋值
                model.rowHeight = bottomToolView.frame.maxY
            }
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        contentLblWidth.constant = screenWidth - 2*margin
       
    }
}

//MARK:========== 计算集合视图的宽度和高度 ==========
extension HomeCell{
    func calculateSize(_ count:Int) -> CGSize {
        //1.没有图片
        if count == 0 {
            collBot.constant = 0
            return CGSize.zero
        }
        
        //1.1改变集合视图的底部约束
        collBot.constant = 10
        
        //2.获取集合视图的布局
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout //强转为流式布局
        layout.itemSize.width = itemWidth
        layout.itemSize.height = itemWidth
        
        //3.单张图片
        if count == 1 {
            if let url = model?.imaURLs?.first?.absoluteString  {
                //3.1取出图片
                let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: url)
                
                //3.2设置集合视图的布局
                layout.itemSize.width = (image?.size.width)!
                layout.itemSize.height = (image?.size.height)!
                
                //3.3返回集合视图的大小
                return CGSize(width: image!.size.width, height: image!.size.height)
            }
        }
        
        //4.count = 4
        if count == 4 {
            let viewW = 2*itemWidth + itemMargin
            return CGSize(width: viewW, height: viewW)
        }
        
        //5.其他张图片
        let rows = CGFloat((count - 1)/3 + 1)
        let viewW = screenWidth - 2*margin
        let viewH = rows*itemWidth + (rows - 1)*itemMargin
        
        return CGSize(width: viewW, height: viewH)
        
    }
}
